﻿using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            //Process[] p = Process.GetProcesses();//获取进程信息
            //Int64 totalMem = 0;
            //string info = "";
            //string cpu = "";
            //foreach (Process pr in p)
            //{
            //    //totalMem += pr.WorkingSet64 / 1024;
            //    info += pr.ProcessName + "内存：-----------" + (pr.WorkingSet64 / 1024).ToString() + "KB\r\n+";//得到进程内存


            //}
            //Console.WriteLine(info);

            var psi = new ProcessStartInfo("iotop", "-b 1") { RedirectStandardOutput = true };
            //启动
            var proc = Process.Start(psi);
            if (proc == null)
            {
                Console.WriteLine("Can not exec.");
            }
            else
            {
                using (var sr = proc.StandardOutput)
                {
                    while (!sr.EndOfStream)
                    {
                        string str = sr.ReadLine();
                        if (str.Contains("Total"))
                        {
                            //var list = str.Replace("%Cpu(s):", "").Replace(" ", "").Split(@",");
                            Console.WriteLine(str);
                        }
                        else if (!str.Contains("PID") && !str.Contains("total") && !str.Contains("top"))
                        {
                            //str = str.Trim();
                            //Regex reg = new Regex(@"\s+");
                            //string modified = reg.Replace(str, ",");
                            //var list = modified.Split(",");
                            ////Console.WriteLine(str);
                            //foreach (var item in list)
                            //{
                            //    Console.Write(item);
                            //}
                            //Console.WriteLine();
                        }

                    }
                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                }
            }


        }
    }
}
